home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / hdf / unix / examples.lha / examples / ann / file_ann.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-13  |  4.0 KB  |  151 lines

  1. #ifdef RCSID
  2. static char RcsId[] = "@(#)$Revision: 1.1 $";
  3. #endif
  4. /*
  5. $Header: /pita/work/HDF/dev/RCS/test/annotations/file_ann_test.c,v 1.1 90/06/27 11:19:35 mfolk beta $
  6.  
  7. $Log:    file_ann_test.c,v $
  8.  * Revision 1.1  90/06/27  11:19:35  mfolk
  9.  * Initial revision
  10.  * 
  11. */
  12. /******************************************************************
  13. *
  14. * file_ann_test.c
  15. *
  16. * Example program: Storing and retrieving file IDs and descriptions.
  17. *
  18. * Opens an already-existing HDF file and stores four file IDs
  19. * and one file description then closes the file.
  20. *
  21. * Reopens the same file and reads back the file IDs and descriptions.
  22. *
  23. *******************************************************************/
  24.  
  25. #include "df.h"
  26. #include "dfan.h"
  27.  
  28. #define MAXLABLEN 80
  29. #define MAXDESCLEN 1000
  30. #define FIRST      1
  31. #define NOTFIRST   0
  32. main (argc, argv)
  33. int argc;
  34. char *argv[];
  35. {
  36.     DF *dfile;
  37.     int i, ret, length;
  38.     char outlabel[MAXLABLEN+1], inlabel[MAXLABLEN+1],
  39.          outdescr[MAXDESCLEN+1], indescr[MAXDESCLEN+1];
  40.  
  41.     DFerror = DFE_NOERROR;
  42.  
  43.     if (argc != 2) {
  44.         printf("Usage: %s filename\n",argv[0]);
  45.         exit(1);
  46.     }
  47.  
  48.     /* store four file IDs in file */
  49.  
  50.     if (NULL==(dfile = DFopen(argv[1], DFACC_WRITE, 0)) ) 
  51.         fatalerror("Error opening file.");
  52.     
  53.     strcpy (outlabel, "Label # ");
  54.  
  55.     for (i=0; i<4; i++) {
  56.         outlabel[7] = '0' + (char) i;
  57.         if ( DFANaddfid(dfile, outlabel) < 0) 
  58.         fatalerror("Error adding label to file.");
  59.     }
  60.  
  61.  
  62.     /* get and store description in file */
  63.  
  64.     getdescr(outdescr);
  65.  
  66.     if ( DFANaddfds(dfile, outdescr, strlen(outdescr)) < 0) 
  67.         fatalerror("Error adding description.");
  68.  
  69.  
  70.     DFclose(dfile);
  71.  
  72.  
  73.     /* open file to read file IDs and file description */
  74.     if (NULL==(dfile = DFopen(argv[1], DFACC_READ, 0)) ) 
  75.         fatalerror("Error opening file.");
  76.  
  77.     /*  read all file IDs from file */
  78.  
  79.     printf("\n\n***Now reading label lengths and labels***\n");
  80.  
  81.     length = DFANgetfidlen(dfile, FIRST);
  82.     if ( (ret = DFANgetfid(dfile,inlabel, MAXLABLEN, FIRST)) < 0 ) 
  83.         fatalerror("Error reading label.");
  84.  
  85.     while ( ret >= 0) {
  86.         printf("Label length: %d\tret: %d\tLabel: %s\n",length,ret,inlabel);
  87.         length = DFANgetfidlen(dfile, NOTFIRST);
  88.         ret = DFANgetfid(dfile,inlabel,MAXLABLEN, NOTFIRST);
  89.     }
  90.     if ( DFerror!=DFE_NOMATCH) 
  91.         fatalerror("Error reading label."); 
  92.  
  93.     printf("\n***End of labels***\n");
  94.  
  95.  
  96.     /* read description length and description from file */
  97.  
  98.     length = DFANgetfdslen(dfile, FIRST);
  99.     printf("\n\nDescription length: %d\n", length);
  100.  
  101.     if ( (ret = DFANgetfds(dfile,indescr, MAXDESCLEN, FIRST)) < 0)
  102.         fatalerror("Error reading description.");
  103.     printf("\n***Just read description. ret: %d\nDescription: \n%s\n",
  104.                                                             ret,indescr);
  105.     printf("\n***End of description***\n");
  106.  
  107.  
  108.     DFclose(dfile);
  109. }
  110.  
  111. /************************************************************
  112. * fatalerror: function to report fatal error and abort
  113. *
  114. ***********************************************************/
  115.     int
  116. fatalerror(s)
  117. char *s;
  118. {
  119.     printf("%s DFerror: %d.\nProgram aborted.\n", s, DFerror);
  120.     exit(1);
  121. }
  122.  
  123. /***********************************************************
  124. *
  125. * getdescr: function to put description in an array
  126. *
  127. ***********************************************************/
  128.     int
  129. getdescr(outdescr)
  130. char *outdescr;
  131. {
  132.     strcpy(outdescr, "Here is the loop used to write labels to this file:\n\n");
  133.     strcat(outdescr, 
  134.      "for (i=0; i<4; i++) {\n");
  135.     strcat(outdescr, 
  136.      " outlabel[7] = '0' + (char) i;\n");
  137.     strcat(outdescr, 
  138.      " if (DFANaddfileann(dfile,outlabel,strlen(outlabel),DFAN_LABEL)<0){\n");
  139.     strcat(outdescr, 
  140.      "   printf(\"Error adding label. DFerror: %d.\\nProgram aborted.\\n\",\n");
  141.     strcat(outdescr, 
  142.      "                                                           DFerror);\n");
  143.     strcat(outdescr, 
  144.      "       exit(1);\n");
  145.     strcat(outdescr, 
  146.      "   }\n");
  147.     strcat(outdescr, 
  148.      "}\n");
  149.     strcat(outdescr, "\nThis is the end of the description.\n\n");
  150. }
  151.